// 人才申报系统控制器类
package controller;

import model.Application;
import model.User;
import service.ApplicationService;
import service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class TalentApplicationController {

    @Autowired
    private UserService userService;
    @Autowired
    private ApplicationService applicationService;

    @PostMapping("/register")
    public Map<String, Object> register(@RequestBody User user) {
        return userService.register(user);
    }

    @PostMapping("/login")
    public Map<String, Object> login(@RequestBody User user) {
        return userService.login(user);
    }

    @PostMapping("/apply")
    public Map<String, Object> submitApplication(@RequestBody Map<String, Object> request) {
        String username = (String) request.get("applicant");
        String content = (String) request.get("content");
        User applicant = new User();
        applicant.setUsername(username);
        return applicationService.submitApplication(applicant, content);
    }

    @GetMapping("/reviews")
    public List<Application> getReviews() {
        return applicationService.getApplicationsByStatus(0);
    }

    @PostMapping("/review")
    public Map<String, Object> conductReview(@RequestBody Map<String, Object> request) {
        int id = (int) request.get("id");
        String applicantUsername = (String) request.get("applicant");
        String content = (String) request.get("content");
        boolean isApproved = (boolean) request.get("isApproved");
        String comment = (String) request.get("comment");
        User reviewer = new User();
        reviewer.setUsername("reviewer"); // 这里简单模拟，实际需要从会话获取
        Application application = new Application();
        application.setId(id);
        application.setApplicant(new User());
        application.getApplicant().setUsername(applicantUsername);
        application.setContent(content);
        return applicationService.conductReview(reviewer, application, 1, isApproved, comment);
    }

    public void start() {
    }
}